from json import load

"""
    描述：数据模块
    功能：清洗关于疫情的数据，并提供接口
"""


# 清洗不规范名称
def format_name_of_province(province_name):
    return province_name.replace('自治区', '').replace('壮族', '').replace('回族', '').replace('维吾尔', '')


class EpidemicSituation:

    def __init__(self):
        self.cure_rate = 0
        self.death_rate = 0
        self.danger_areas = []
        self.danger_city_name = []
        self.suspected_city_name = []
        self.suspected_count = []
        self.suspected_count_geo = []
        self.serious_count = []
        self.suspected_count_currency = []
        self.current_confirm_nums = []
        self.current_serious_nums = []
        self.current_confirm_nums_with_city = []
        self.current_serious_nums_with_city = []
        self.city_name_list = []
        self.danger_city_value = []
        self.danger_city_high_level = []
        self.cov_nums = {}
        self.cov_date = []
        self.current_confirm = []
        self.danger_city_mid_level = []
        self.format_data()
        self.count_cov_nums()
        self.cal_rate()
        self.count_suspected_data()

    def count_detail_info_of_danger_areas(self, danger_areas):
        mid_level_count = 0
        high_level_count = 0
        for i in danger_areas:
            if i["dangerLevel"] == 2:
                mid_level_count += 1
                continue
            high_level_count += 1
        self.danger_city_high_level.append(high_level_count)
        self.danger_city_mid_level.append(mid_level_count)

    def count_cov_nums(self):
        with open('./data/cov_num.json', 'r', encoding='utf-8') as f:
            cov_data = load(f)
            self.cov_nums = cov_data[-1]
            for i in range(0, len(cov_data)):
                self.suspected_count.append(cov_data[i]['suspectedCount'])
                self.cov_date.append(cov_data[i]['date'])
                self.current_confirm.append(cov_data[i]['currentConfirmedCount'])
                self.serious_count.append(cov_data[i]['seriousCount'])

    def format_data(self):
        with open('./data/cov_info_of_china.json', 'r', encoding='utf-8') as fp:
            json_data = load(fp)
        file_len = len(json_data)
        for i in range(0, file_len):
            lengthy_of_danger = len(json_data[i]['dangerAreas'])  # dangerAreas信息存放在列表，通过长度判断是否为空
            current_num = json_data[i]['currentConfirmedCount']
            serious_num = json_data[i]['suspectedCount']

            city_name = format_name_of_province(json_data[i]['provinceName'])  # 格式化省份名称，避免地图上画不出
            self.city_name_list.append(city_name)
            # 清洗好的数据放在列表里面
            if lengthy_of_danger != 0:
                self.danger_city_value.append(lengthy_of_danger)
                self.danger_city_name.append(city_name)
                self.count_detail_info_of_danger_areas(json_data[i]['dangerAreas'])
            if current_num != 0:
                self.current_confirm_nums.append(current_num)
            if serious_num != 0:
                self.current_serious_nums.append(serious_num)

        self.danger_areas = list(zip(self.danger_city_name, self.danger_city_value))
        self.current_confirm_nums_with_city = list(zip(self.current_confirm_nums,  self.city_name_list))
        self.current_serious_nums_with_city = list(zip(self.city_name_list,  self.current_serious_nums))
        self.current_confirm_nums_with_city.sort(reverse=True)  # 降序排列

    # 清洗比较繁琐，单独拎出来写
    def count_suspected_data(self):
        with open('./data/cov_info_of_china.json', 'r', encoding='utf-8') as fp:
            json_data = load(fp)
        file_len = len(json_data)
        count = 0
        for i in range(0, file_len):
            lengthy_of_cities = len(json_data[i]['cities'])
            if lengthy_of_cities == 0:
                continue
            j = 0
            while j < lengthy_of_cities:
                city_dict = json_data[i]['cities'][j]
                if city_dict['cityName'] != '境外输入':
                    j += 1
                    continue
                elif city_dict['currentConfirmedCountStr'].isdigit() is False:
                    j += 1
                    continue
                elif eval(city_dict['currentConfirmedCountStr']) == 0:
                    j += 1
                    continue
                else:
                    count += eval(city_dict['currentConfirmedCountStr'])
                    self.suspected_city_name.append(self.city_name_list[i])
                    self.suspected_count_geo.append(eval(city_dict['currentConfirmedCountStr']))
                    break

    def count_suspected_currency(self):
        currency = []
        lengthy = len(self.suspected_count)
        i = 0
        while i < lengthy - 1:
            tem = self.suspected_count[i + 1] - self.suspected_count[i]
            if tem < 0:
                tem = 0
            currency.append(tem)
            i += 1
        return currency

    def count_serious_currency(self):
        currency = []
        lengthy = len(self.serious_count)
        i = 0
        while i < lengthy - 1:
            currency.append(self.serious_count[i + 1] - self.serious_count[i])
            i += 1
        return currency

    def cal_rate(self):
        self.cure_rate = self.cov_nums['cureCount'] / self.cov_nums['confirmedCount']
        self.death_rate = self.cov_nums['deadCount'] / (self.cov_nums['deadCount'] + self.cov_nums['cureCount'])
